﻿/*************************************************************
 * Software of blackcore
 *
 * CLRVersion:4.0.30319.586
 * MachineName:SPACE
 * Author:Andy Huang
 * Email:itblackhole@gmail.com
 * DateTime：6/20/2013 10:07:31 AM
 * Function：
 *************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SobFS.Common;
using System.Data;

namespace SobFS.Web.backend.manage.functionmenu
{
    public partial class Edit : SobFS.Web.UI.BackendPage
    {
        #region ******************************Private Property******************************
        private int id = 0;
        #endregion ******************************Private Property******************************


        #region ******************************Public Property******************************
        #endregion ******************************Public Property******************************


        #region ******************************Structure******************************
        protected void Page_Load(object sender, EventArgs e)
        {
            //TODO 权限检查
            this.id = RequestHelper.GetQueryInt("Id");
            if (!Page.IsPostBack)
            {
                BindMenuType();
                LoadDataById();
            }
        }
        #endregion  ******************************Structure******************************


        #region ******************************Private Method******************************
        void BindMenuType()
        {
            foreach (int value in Enum.GetValues(typeof(SobFSConst.MenuType)))
            {
                string menuTypeName = Enum.GetName(typeof(SobFSConst.MenuType), value);
                ListItem item = new ListItem(menuTypeName, value.ToString());
                this.ddlMenuType.Items.Add(item);
            }
        }

        void BindFunctionMenuTree(string menuType)
        {
            BLL.SysFunctionMenu bll = new BLL.SysFunctionMenu();
            DataSet ds = bll.GetList(string.Format("MenuType = '{0}' AND IsObsoleted = 0 ", menuType));
            if (ds != null && ds.Tables != null && ds.Tables.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                this.ddtParent.Bind(dt);
            }
        }

        void LoadDataById()
        {
            Model.SysFunctionMenu model = new BLL.SysFunctionMenu().GetModel(this.id);
            if (model != null)
            {
                this.ddlMenuType.SelectedItem.Text = model.MenuType;
                BindFunctionMenuTree(model.MenuType);
                this.ddtParent.SelectedValue = model.ParentId.ToString();
                this.rblIsEnabled.SelectedValue = model.IsEnabled ? "1" : "0";
                this.txtCode.Text = model.Code;
                this.txtName.Text = model.Name;
                this.txtURL.Text = model.URL;
                this.txtSort.Text = model.Position == null ? string.Empty : model.Position.ToString();
                this.txtDescription.Text = model.Description;
            }
            else
            {
                JsMessageBox("数据加载失败！", "List.aspx", UI.JsMessageBoxCss.Error);
            }
        }
        #endregion ******************************Private Method******************************


        #region ******************************Public Method******************************
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            Model.SysFunctionMenu model = new BLL.SysFunctionMenu().GetModel(this.id);
            if (model == null)
            {
                JsMessageBox("操作失败，未获取正确数据！", string.Empty, UI.JsMessageBoxCss.Error);
                return;
            }

            model.ParentId = this.ddtParent.NodeId;
            model.IsEnabled = int.Parse(this.rblIsEnabled.SelectedValue) == 0 ? false : true;
            model.Code = this.txtCode.Text.Trim();
            model.Name = this.txtName.Text.Trim();
            model.Position = int.Parse(this.txtSort.Text.Trim());
            model.URL = this.txtURL.Text.Trim();
            model.Description = this.txtDescription.Text.Trim();
            if (new BLL.SysFunctionMenu().Update(model))
            {
                JsMessageBox("操作成功！", "List.aspx", UI.JsMessageBoxCss.Success);
            }
            else
            {
                JsMessageBox("操作失败！", string.Empty, UI.JsMessageBoxCss.Error);
            }
        }

        protected void ddlMenuType_SelectedIndexChanged(object sender, EventArgs e)
        {
            string menuType = this.ddlMenuType.SelectedItem.Text;
            if (!string.IsNullOrWhiteSpace(menuType))
            {
                this.BindFunctionMenuTree(menuType);
            }
        }
        #endregion ******************************Public Method******************************


    }
}